package com.jvyou.app.ds.utils;

import com.jvyou.app.common.constants.BaseConstants;
import com.jvyou.app.ds.domain.entity.Question;
import com.jvyou.app.ds.domain.entity.QuestionOption;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class QuestionParser {

    private static final Pattern QUESTION_PATTERN = Pattern.compile(
            "^\\[(.*?)\\]\\[(A\\.)(.*?)\\]\\[(B\\.)(.*?)\\]\\[(C\\.)(.*?)\\]\\[(D\\.)(.*?)\\]\\[(.*?)\\]\\[(.*?)\\]\\[(.*?)\\]$"
    );

    /**
     * 解析AI生成的题目内容
     *
     * @param aiContent AI生成的完整题目内容
     * @return 包含Question和QuestionOption列表的包装对象
     */
    public static QuestionParseResult parseFromAiContent(String aiContent) {
        // 去除换行和空白
        aiContent = aiContent.replaceAll("\\s+", "");
        log.info("开始解析AI生成的题目内容: {}", aiContent);
        Matcher matcher = QUESTION_PATTERN.matcher(aiContent);
        if (!matcher.matches()) {
            log.error("解析失败。题目内容格式不正确: {}", aiContent);
            return null;
        }

        // 解析题目基本信息
        Question question = new Question();
        question.setContent(matcher.group(1)); // 题目内容
        question.setExplanation(matcher.group(10)); // 解析内容
        question.setKeyPoint(matcher.group(11)); // 考点内容
        String correctAnswer = matcher.group(12); // 正确答案(如"C")

        // 设置默认值
        question.setQuestionType("1"); // 1-单选
        question.setLicenseTypeId(6L); // 科目一
        question.setCourseTypeId(1L); // 科目一
        question.setDifficulty("2"); // 中等难度
        question.setStatus(BaseConstants.NORMAL); // 启用
        question.setSource("ai"); // AI生成
        question.setTypeId(26L); // AI生成

        // 解析选项
        List<QuestionOption> options = new ArrayList<>();
        options.add(createOption("A", matcher.group(3), correctAnswer, 1));
        options.add(createOption("B", matcher.group(5), correctAnswer, 2));
        options.add(createOption("C", matcher.group(7), correctAnswer, 3));
        options.add(createOption("D", matcher.group(9), correctAnswer, 4));
        log.info("解析成功。题目内容: {}, 选项: {}", question.getContent(), options);
        return new QuestionParseResult(question, options);
    }

    /**
     * 创建选项对象
     */
    private static QuestionOption createOption(String code, String content,
                                               String correctAnswer, int order) {
        QuestionOption option = new QuestionOption();
        option.setOptionCode(code);
        option.setContent(content);
        option.setIsCorrect(code.equals(correctAnswer) ? "0" : "1");
        option.setSortOrder(order);
        return option;
    }

    /**
     * 解析结果包装类
     */
    public static class QuestionParseResult {
        private final Question question;
        private final List<QuestionOption> options;

        public QuestionParseResult(Question question, List<QuestionOption> options) {
            this.question = question;
            this.options = options;
        }

        // getter方法
        public Question getQuestion() {
            return question;
        }

        public List<QuestionOption> getOptions() {
            return options;
        }
    }
}